package com.example.module_main.dialog

import android.graphics.Color
import android.os.Bundle
import android.provider.CalendarContract.Colors
import android.text.SpannableStringBuilder
import android.text.method.LinkMovementMethod
import android.util.DisplayMetrics
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import com.example.module_main.R
import com.example.module_main.databinding.DialogMessageNoticeBinding

/**
 * @author yaotianxue
 * @description 温馨提示对话框
 * @date 2025/1/15
 */
class MessageNoticeDialog(
    private val title:String? = null,//标题
    private val message:SpannableStringBuilder? = null,//内容
    private val onConfirmClick:(()->Unit)? = null,//确定的回调函数
    private val onCancelClick:(()->Unit)? = null,//取消的回调函数
    private val onlyConfirmButton:Boolean = false, //是否只有确认按钮
    private val confirmText:String = "确定",
    private val cancelText:String = "取消",
    private val alignment:Int = Gravity.CENTER,//文字的对其方式
    private val cancelable:Boolean = true,//是否可取消
    private val confirmClickDismiss:Boolean = true //点击确认按钮后是否关闭对话框
):DialogFragment(){

    constructor(
      title:String? = null,//标题
      message:String? = null,//内容
      onConfirmClick:(()->Unit)? = null,//确定的回调函数
      onCancelClick:(()->Unit)? = null,//取消的回调函数
      onlyConfirmButton:Boolean = false, //是否只有确认按钮
      confirmText:String = "确定",
      cancelText:String = "取消",
      alignment:Int = Gravity.CENTER,//位置
      cancelable:Boolean = true,//是否可取消
      confirmClickDismiss:Boolean = true //点击确认按钮后是否关闭对话框
    ):this(
        title,
        SpannableStringBuilder(message),
        onConfirmClick,
        onCancelClick,
        onlyConfirmButton,
        confirmText,
        cancelText,
        alignment,
        cancelable,
        confirmClickDismiss
    )

    private lateinit var mParentView: View
    private lateinit var mBinding: DialogMessageNoticeBinding
    companion object{
        val TAG = MessageNoticeDialog::class.java.simpleName
    }
    //创建
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NO_TITLE,R.style.MessageNoticeDialog)
    }
    /**
     * 调整对话框的宽高和位置
     */
    override fun onStart() {
        super.onStart()
        //获得对话框的属性
        val window = dialog?.window
        val attributes = window?.attributes
        //获得窗口的宽度和高度
        val dm = DisplayMetrics()
        activity?.windowManager?.defaultDisplay?.getMetrics(dm)
        window?.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)
        //修改对话框的属性并重新设置给当前对话框
        attributes?.width = (dm.widthPixels*0.7).toInt()
        attributes?.gravity = Gravity.CENTER
        window?.attributes = attributes
    }

    /**
     * 绑定view视图
     */
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        mBinding = DialogMessageNoticeBinding.inflate(inflater,container,false)
        mParentView = mBinding.root
        return mParentView
    }

    /**
     * 设置数据源
     */
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        dialog?.setCancelable(cancelable)
        dialog?.setCanceledOnTouchOutside(cancelable)

        mBinding.tvTitle.apply {
            if(title == null){
                visibility = View.GONE
            }else{
                visibility = View.VISIBLE
                text = title
            }
        }
        mBinding.btnCancel.text = cancelText
        mBinding.btnConfirm.text = confirmText

        mBinding.tvMessage.apply {
            text = message
            gravity = alignment
            highlightColor = Color.TRANSPARENT //富文本点击高亮显示设置为透明
            movementMethod = LinkMovementMethod.getInstance() //富文本点击事件不起作用
        }
        //是否只显示确认按钮
        if(onlyConfirmButton){
            mBinding.line.visibility = View.GONE
            mBinding.btnCancel.visibility = View.GONE
        }else{
            mBinding.line.visibility = View.VISIBLE
            mBinding.btnCancel.visibility = View.VISIBLE
            mBinding.btnCancel.setOnClickListener{
                onCancelClick?.invoke()
                dismiss()
            }
        }
        mBinding.btnConfirm.setOnClickListener {
            onConfirmClick?.invoke()
            if(confirmClickDismiss){
                dismiss()
            }
        }

    }

    /**
     * 打开对话框
     */
    fun show(fragmentManager: FragmentManager){
        if(fragmentManager.findFragmentByTag(TAG)?.isVisible === true){
            return
        }
        this.show(fragmentManager, TAG)
    }






}